﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Notepad
{
    public partial class Form1 : Form
    {
        System.Drawing.Printing.PrintDocument prntDoc = new System.Drawing.Printing.PrintDocument();
        public Form1()
        {
            InitializeComponent();
        }

        private void newBtn_Click(object sender, EventArgs e)
        {
            fileTxt.Clear();
        }

        private void saveBtn_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            string sfdName = sfd.FileName;
            string sfdFilter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
            sfd.Filter = sfdFilter;
            sfd.Title = "Save";
            if (sfd.ShowDialog(this) == DialogResult.OK)
            {
                System.IO.File.WriteAllText(sfd.FileName, fileTxt.Text);
            }
            else
            {
                return;
            }

        }

        private void openBtn_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.FileName = "";
            String ofdName = ofd.FileName;
            String ofdFilter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
            ofd.Filter = ofdFilter;
            ofd.Title = "Open";
            if (ofd.ShowDialog(this) == DialogResult.OK)
            {
                fileTxt.Text = System.IO.File.ReadAllText(ofd.FileName);
            }
            else
            {
                return;
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
                
        }

        private void printBtn_Click(object sender, EventArgs e)
        {
            PrintDialog print = new PrintDialog();
            prntDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(prntDoc_PrintPage);
            print.Document = prntDoc;
            if (print.ShowDialog(this) == DialogResult.OK)
            {
                prntDoc.Print();
            }

        }
        private void prntDoc_PrintPage(Object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            //Declare g as Graphics equal to the PrintPageEventArgs Graphics
            Graphics g = e.Graphics;
            //Draw the Text in txtBox to the Document
            g.DrawString(fileTxt.Text, fileTxt.Font, Brushes.Black, 0, 0);
        }

        private void printPreviewBtn_Click(object sender, EventArgs e)
        {
            PrintPreviewDialog preview = new PrintPreviewDialog();
            prntDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(prntDoc_PrintPage);
            preview.Document = prntDoc;
            if (preview.ShowDialog(this) == DialogResult.OK)
            {
                prntDoc.Print();
            }

        }

        private void copyBtn_Click(object sender, EventArgs e)
        {
            fileTxt.Copy();
        }

        private void cutBtn_Click(object sender, EventArgs e)
        {
            fileTxt.Cut();
        }

        private void pasteBtn_Click(object sender, EventArgs e)
        {
            fileTxt.Paste();
        }

        private void undoBtn_Click(object sender, EventArgs e)
        {
            fileTxt.Undo();
        }

    }
}
